package com.bmw.imageBlog.controller;

import com.bmw.imageBlog.bmImage.domain.BmImage;
import com.bmw.imageBlog.bmImage.service.BmImageService;
import com.bmw.imageBlog.bmMedia.model.BmMedia;
import com.bmw.imageBlog.bmMedia.service.BmMediaService;
import com.bmw.util.FileIoUtils;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.io.File;
import java.util.List;

/**
 * Created by Administrator on 2018/11/26.
 */
@Controller
@RequestMapping("imageBlog")
public class imageBlogController {

    @Autowired
    BmMediaService bmMediaService;

    @Autowired
    BmImageService bmImageService;

    @GetMapping("index")
    public String index(){
        return "imageBlog/index.html";
    }

    @GetMapping("getMedia/{type}/{pageNum}")
    @ResponseBody
    public List<BmMedia> getMedia(@PathVariable(value="type",required=false) String type,@PathVariable(value="pageNum",required=false)Integer pageNum){
        BmMedia bm = new BmMedia();
        bm.setType(type);
        int feny= 1;
//        if(feny == 1){
//
//        }else if(feny == 2){
//
//        }
        return bmMediaService.findLike(bm);
    }

    @GetMapping("getImage/{type}/{pageNum}")
    @ResponseBody
    public List<BmImage> getImage(@PathVariable(value="type",required=false) String type,@PathVariable(value="pageNum",required=false)Integer pageNum){
        if(pageNum==null){
            pageNum=1;
        }
        PageHelper.startPage(pageNum,3);
        BmImage bm = new BmImage();
        bm.setType(type);
        return bmImageService.findLike(bm);
    }

    @GetMapping("insertImage")
    @ResponseBody
    public void getImage(){
        FileIoUtils file = new FileIoUtils();
        List<String> list = file.file(new File("G:\\xx\\im\\cc"));
        for(String li:list){
            BmImage bm = new BmImage();
            bm.setName(li);
            bm.setLevel(1);
            bm.setUrl("http://localhost:8081/images/"+li);
            bm.setType("1");
            bmImageService.add(bm);
        }
    }

    @GetMapping("insertMedia")
    @ResponseBody
    public void getMedia(){
        FileIoUtils file = new FileIoUtils();
        List<String> list = file.file(new File("G:\\xx\\video\\smallVideo"));
        for(String li:list){
            BmMedia bm = new BmMedia();
            bm.setName(li);
            bm.setLevel(1);
            bm.setUrl("http://localhost:8081/smallVideo/"+li);
            bm.setType("1");
            bmMediaService.add(bm);
        }
    }

}
